<template>
  <div>
    <div class="container" style="height: 100vh">
      <div class="login-wrapper">
        <div class="header">用户登录</div>
        <div class="form-wrapper">
          <input
            v-model="rname"
            type="text"
            name="username"
            placeholder="请输入用户名"
            class="input-item"
            @blur="checkName"
          />
          <span :style="{ visibility: isVisibility11 }">用户名错误</span>

          <input
            v-model="rpwd"
            type="password"
            name="password"
            placeholder="请输入密码"
            class="input-item"
            @blur="checkPwd"
          />
          <span :style="{ visibility: isVisibility12 }">输入密码错误</span>

          <!-- <input @click="login" class="btn" type="button" value="登录" /> -->
          <template>
            <div :plain="true" @click="login" class="btn">登录</div>
          </template>
        </div>
        <div class="msg">
          没有账户?
          <router-link to="/register">注册</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  // mounted () {
  //     this.login();
  // },

  data() {
    return {
      rname: "",
      rpwd: "",
      // 保存获取到的token的值
      token: "",

      isVisibility11: "hidden",
      isVisibility12: "hidden",
    };
  },
  methods: {
    login() {
      const url = "user/login";
      const params = `rname=${this.rname}&rpwd=${this.rpwd}`;
      this.axios.post(url, params).then(res => {
        console.log("登录页面数据:", res);
        if (res.data.code == 200) {
          this.$store.commit("updateRname", this.rname);

          sessionStorage.setItem("token", "121212131");

          this.$message({
            message: "恭喜您，登录成功",
            type: "success",
          });
          //如果用户是从首页详情  进入的登录页,登陆成功后,返回上一级页页面
          //否则,返回首页
          if (this.$route.query.idd == 1) {
            this.$router.go(-1);
          } else {
            this.$router.push("/");
          }
        } else {
          this.$message.error("用户名或密码错误");
        }
      });
    },
    checkName() {
      let exp = /^[\u4e00-\u9fa5]{0,}$/;
      if (exp.test(this.rname)) {
        this.isVisibility11 = "hidden";
        return true;
      } else {
        this.isVisibility11 = "visible";
        return false;
      }
    },
    checkPwd() {
      let exp = /^\d{6}$/;
      if (exp.test(this.rpwd)) {
        this.isVisibility12 = "hidden";
        return true;
      } else {
        this.isVisibility12 = "visible";
        return false;
      }
    },
  },
};
</script>
<style scoped>
.container {
  height: 100%;
  background-image: linear-gradient(to right, #fbc2eb, #a6c1ee);
}
.login-wrapper {
  background-color: #fff;
  width: 358px;
  height: 558px;
  border-radius: 15px;
  padding: 0 50px;
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.header {
  font-size: 38px;
  font-weight: bold;
  text-align: center;
  line-height: 200px;
}
.input-item {
  display: block;
  width: 100%;
  margin-bottom: 20px;
  border: 0;
  padding: 10px;
  border-bottom: 1px solid rgb(128, 125, 125);
  font-size: 15px;
  outline: none;
}
.input-item:placeholder {
  text-transform: uppercase;
}
.btn {
  text-align: center;
  padding: 10px;
  width: 100%;
  margin-top: 40px;
  background-image: linear-gradient(to right, #a6c1ee, #fbc2eb);
  color: #fff;
  user-select: none;
  border: none;
}
.msg {
  text-align: center;
  line-height: 88px;
}
a {
  text-decoration-line: none;
  color: #abc1ee;
}
</style>

<style lang="scss" scoped></style>
